//
//  MineLookViewController.m
//  Tongcheng
//
//  Created by 邢广范 on 2017/4/6.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//我的-最近来访

#import "MineLookViewController.h"
#import "MJRefresh.h"
#import "MineLookCollectionViewCell.h"
#import "MineLookOperator.h"
#import "MineLookModel.h"
#import "MineLookDataModel.h"
#import "MineLookListModel.h"

@interface MineLookViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, assign) NSInteger page;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *userCountLabel;

@end

@implementation MineLookViewController

static NSString *lookCellIdentifier = @"MineLookCollectionViewCell";

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.collectionView reloadData];
}

- (NSInteger)page
{
    if (!_page) {
        _page = 1;
    }
    return _page;
}

- (void)setupNavigation
{
    self.navigationItem.title = @"最近来访";
}

- (void)setupView
{
    self.titleLabel.text = [NSString stringWithFormat:@"HI %@", self.config.userName];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((kScreenWidth - 90) / 4, (kScreenWidth - 90) / 4);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 30, 5, 30);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 177, kScreenWidth, kScreenHeight - 241) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = YES;
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"MineLookCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:lookCellIdentifier];
    [self.view addSubview: self.collectionView];
    
//    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        self.page = 1;
//        [self download];
//    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigation];
    [self setupView];
    [self download];
    // Do any additional setup after loading the view from its nib.
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MineLookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:lookCellIdentifier forIndexPath:indexPath];
    MineLookListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Page"] = [NSString stringWithFormat:@"%ld", self.page];
    dic[@"Row"] = @"30";
    MineLookOperator *operator = [[MineLookOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataArray = operator.data.data.list;
            self.userCountLabel.text = [NSString stringWithFormat:@"最近有%ld个用户来看过您", self.dataArray.count];
        }
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
